#include <getopt.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//#include "service.h"
#include "benchapi.h"

#define  VERSION   "2011.1.3"

extern "C" int handle_init (int argc, char **argv, int pid_type)
{
	int c;

	switch (pid_type) {
		case PROC_MAIN:
			while ((c = getopt (argc, argv, "s:")) != -1) {
				if (c == 's')
					bufsz = atoi (optarg);
			}
			if (bufsz <= 0)
				ERROR_RETURN (("invalid buffer size:%s", optarg), -1);
			DEBUG_LOG ("main process init success");

            SetVersion(VERSION);
			return 0;
		case PROC_WORK:
			return 0;
		case PROC_CONN:
			return 0;
		default:
			ERROR_LOG ("invalid pid_type=%d", pid_type);
			return -1;
	}

	return -1;
}


extern "C" int handle_input (const char* buffer, int length, const skinfo_t *sk)
{
	struct in_addr addr;
	struct in_addr local_addr;

	addr.s_addr = sk->remote_ip;
	local_addr.s_addr = sk->local_ip;
	
    /*
	fprintf (stderr,"package strace %s:%u->%s:%u , length=%d \n",
		inet_ntoa (addr), sk->remote_port,inet_ntoa(local_addr),sk->local_port, length); 
    */
	return length;
}

extern "C" int handle_process (char *recvbuf, int rcvlen, const skinfo_t *sk , struct shm_block* mb)
{
	struct in_addr addr;

	addr.s_addr = sk->remote_ip;
	char recvtm[32];
	strftime(recvtm,sizeof(recvtm),"%Y-%m-%d %H:%M:%S",localtime(&(sk->tasktm)));	
    /*
	DEBUG_LOG ("recv from %s:%u %d bytes, package length=%d recvtm=%s",
		inet_ntoa (addr), sk->remote_port, rcvlen, rcvlen,recvtm); 
    */
	
    sendToClient(recvbuf , rcvlen , mb);
    sendToClient("test haha" , 10 , mb);
	return 0;
}

